package com.xm.xmapp.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.customview.LoadingDialog;
import com.xm.xmapp.tools.AndtoidRomUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public abstract class BaseActivity extends FragmentActivity implements BaseInterFace {

	private FrameLayout flContent;
	public LoadingDialog promptDialog;
	/**
	 * 是否设置根布局pad距离
	 */
	public boolean isUsePad = true;
	public Intent intent;
	public boolean isTestMemory = false;

	public void setMyTitle(String title) {
		TextView t = (TextView) findViewById(R.id.title);
		if (t != null) {
			t.setText(title);
		}
		View v = findViewById(R.id.back);
		if (v != null) {
			v.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getLastData(savedInstanceState);
		promptDialog = new LoadingDialog(this);
		MyAppcation.getMyAppcation().addBaseInterFace(this);

		if(AndtoidRomUtil.isMIUI()) {
			Class<? extends Window> clazz = getWindow().getClass();
			try {
				int darkModeFlag = 0;
				Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
				Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
				darkModeFlag = field.getInt(layoutParams);
				Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
				extraFlagField.invoke(getWindow(), true ? darkModeFlag : 0, darkModeFlag);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		super.setContentView(R.layout.activity_base);
		flContent = (FrameLayout) findViewById(R.id.content);
	}

	public void ViewTreeObserver() {

	}

	public void getLastData(Bundle savedInstanceState) {

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveData(outState);
	}

	public void saveData(Bundle outState) {

	}

	@Override
	public void setContentView(int layoutResID) {

		View content = getLayoutInflater().inflate(layoutResID, null);
		flContent.removeAllViews();
		flContent.addView(content, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ViewTreeObserver vto2 = flContent.getViewTreeObserver();
		vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void onGlobalLayout() {
				flContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				viewTreeObserver();
			}
		});
		if (!isTestMemory) {
			initView();
			initData();
			initEvent();
		}

	}

	protected void viewTreeObserver() {

	}

	public abstract void initView();

	public abstract void initData();

	public abstract void initEvent();

	@Override
	protected void onDestroy() {
		MyAppcation.getMyAppcation().reMoveBaseInterFace(this);
		MyAppcation.getMyAppcation().reMoveRequest(this);
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
		super.onDestroy();

	}


	@Override
	public void finishActivity() {
		finish();
	}

	@Override
	public Object dataChang(int flag, Object data) {
		return data;
	}

}
